package com.lagou.dao;

import com.lagou.entity.Phone;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class PhoneDao {

    // 需求： 查询价格高于2000元，生产日期是2019年之前的所有手机
    public List<Phone> findPhone(double price, String date) throws SQLException {

        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        // 编写sql
        String sql = "select * from phone where price > ? and prodate < ?";

        // 获取单列数据 使用scalarHandler来封装
        List<Phone> list =  qr.query(sql,new BeanListHandler<Phone>(Phone.class), price,date);

        // 返回
        return list;
    }

    // 需求： 查询所有颜色为白色的手机的信息
    public List<Phone> findPhoneByColor(String color) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

        // 编写sql
        String sql2 = "select * from phone where color = '白色' ";

        // 获取数据 用scalarHandler来封装
        List<Phone> list = qr.query(sql2,new BeanListHandler<Phone>(Phone.class),color);

        // 返回值
        return list;
    }
}
